<?php
declare(strict_types=1);

namespace ImiApp\ApiServer\Backend\Controller;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Phpben\Imi\Validate\Annotation\Validate;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Db\Db;

use ImiApp\ApiServer\Backend\Controller\CommonController as CommonController;

/**
 * @Auth(name="backend")
 * @Controller("/goods/")
 */
class GoodsController extends CommonController
{
    /**
     * @Inject("GoodsService")
     */
    protected $service;

    /**
     * @Action
     * @Route("read")
     * @return ResponseInterface
     */
    public function read(): ResponseInterface
    {
        return $this->response->success(null, $this->service->read() ?: []);
    }

    /**
     * @Action
     * @Route(url="create",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function create($data): ResponseInterface
    {
        return $this->service->create($data) ? $this->response->success('创建成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="update",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function update($data): ResponseInterface
    {

        try {//保存修改日志更新
            $id = $this->request->post('id');
            $goods = Db::name('goods')->where('id','=',$id)->find();
            $goodslog_id = Db::name('goodslog')->insert($goods)->getLastInsertId();


            //循环加入商品价格的数据
            $list = Db::name('goodssku_price')->where('goods_id','=',$id)->select()->getArray();
            if($list){
                foreach ($list as $key => $pricelog) {
                    $pricelog['goodslog_id'] = $goodslog_id;
                    Db::name('goodssku_pricelog')->insert($pricelog);
                }               
            }
        } catch (\Throwable $th) {
            \Imi\Log\Log::error('商品更新失败'.$id.$th->getMessage());
        }

        return $this->service->update($this->request->getParsedBody()) ? $this->response->success('更新成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="delete",method="POST")
     * @return ResponseInterface
     */
    public function delete(): ResponseInterface
    {
        return $this->service->delete() ? $this->response->success('删除成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="operate",method="POST")
     * @return ResponseInterface
    */
    public function operate(): ResponseInterface
    {
        return $this->service->operate() ? $this->response->success('操作成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="sort",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
    */
    public function sort($data): ResponseInterface
    {
        return $this->service->draggable($data) ? $this->response->success('排序成功') : $this->response->error($this->service->getError());
    }    
}